﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess.SqlClient;
using Model;
using System.Data.SqlClient;
using System.Data;
using ApiDAL.Common;
namespace Dal
{
    public class OrderDal
    {
        #region 显示订单
        /// <summary>
        /// 显示订单
        /// </summary>
        /// <returns></returns>
        public List<Order> ShowOrder(int id)
        {
            SqlQuery sql = new SqlQuery();
            sql.CommandText = "select * from [Order] where UserId="+id+"";
            DataSet ds = SqlDataAccess.ExecDataset(sql);
            DataTable dt = ds.Tables[0];
            return DataReaderToObjectHelper.DataTableToList<Order>(dt);
        }
        #endregion

        #region 添加订单
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public int AddOrder(Order n, int carid)
        {
            SqlQuery sql = new SqlQuery();
            sql.CommandText = "insert into [Order] values('" + n.OrderName + "','" + n.OrderPhone + "','" + n.Orderprice + "'," + n.OrderNum + ",'" + n.OrderSum + "','" + n.OrderAddr + "','" + n.OrderDate + "','" + n.OrderStates + "','" + n.OrderPayStates + "'," + n.UserId + ");delete from Car where CarId = " + carid + "";
            return SqlDataAccess.ExecNonQuery(sql);
        }
        #endregion

        #region 添加订单备注
        /// <summary>
        /// 添加订单备注
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public int AddAdvice(string advice,string email,int Userid)
        {
            SqlQuery sql = new SqlQuery();
            sql.CommandText = "insert into Advice values('" + advice + "','" + email + "'," + Userid + ")";
            return SqlDataAccess.ExecNonQuery(sql);
        }
        #endregion

        #region 修改订单
        /// <summary>
        /// 修改订单
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public int UptOrder(Order n)
        {
            SqlQuery sql = new SqlQuery();
            sql.CommandText = "update [order] set OrderName='" + n.OrderName + "',OrderPhone='" + n.OrderPhone + "',Orderprice='" + n.Orderprice + "',OrderNum=" + n.OrderNum + ",OrderSum='" + n.OrderSum + "',OrderAddr='" + n.OrderAddr + "',OrderDate='" + n.OrderDate + "',OrderStates='" + n.OrderStates + "',OrderPayStates='" + n.OrderPayStates + "' where OrderId="+n.OrderId+" ";
            return SqlDataAccess.ExecNonQuery(sql);
        }
        #endregion

        #region 清空订单
        /// <summary>
        /// 清空订单
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public int empty(int id)
        {
            SqlQuery sql = new SqlQuery();
            sql.CommandText = "delete  from [Order] where UserId='" + id + "'";
            return SqlDataAccess.ExecNonQuery(sql);
        }
        #endregion

        #region 删除订单
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="n"></param>
        /// <returns></returns>
        public int DelOrder(string id)
        {
            SqlQuery sql = new SqlQuery();
            sql.CommandText = "delete  from [Order] where OrderId=(" + id + ")";
            return SqlDataAccess.ExecNonQuery(sql);
        }
        #endregion

    }
}
